package sys_ctl

import (
	"net/http"
	"ubt-rbac-service/internal/app/model"
	"ubt-rbac-service/internal/app/model/sys_mod"
	"ubt-rbac-service/internal/app/service/sys_serv"
	"ubt-rbac-service/internal/app/util"
	"ubt-rbac-service/internal/core/convert"
	"ubt-rbac-service/internal/core/parse"
)

type sysTag struct{}

var SysTagCtl sysTag

// Save 保存标签
func (c *sysTag) Save(w http.ResponseWriter, r *http.Request) {
	// 将请求的参数转换为请求结构体
	vo, errStr := parse.Parse(r, &sys_mod.SysTagSaveReq{})
	if vo == nil {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: errStr})
		return
	}
	req := vo.(*sys_mod.SysTagSaveReq)
	// 通过头部请求的token获取用户信息
	token := r.Header.Get("token")
	userInfo := util.GetUserInfo(token)
	if userInfo == nil {
		util.ToJson(w, &model.ResJson{Code: 401, Msg: "对不起，您未登录或登录超时，请登录！"})
		return
	}
	// 过滤关键参数值中的非法字符
	req.TagId = util.InputFilter(req.TagId, true)
	req.TagName = util.InputFilter(req.TagName, true)
	// 判断编码是否重复
	extVo := &sys_mod.SysTagExtVo{TagId: req.TagId, TagName: req.TagName, Kind: req.Kind, OptOrg: userInfo.DeptId}
	if ext := sys_serv.SysTagServ.Exists(extVo); ext {
		util.ToJson(w, &model.ResJson{Code: 402, Msg: "对不起，填写的标签已存在，请更换！"})
		return
	}
	req.OptOrg = userInfo.DeptId
	var (
		result *model.Result
		kind   uint8
	)
	if req.TagId == "" {
		// 新增
		req.TagId = util.NewNanoid()
		result = sys_serv.SysTagServ.Add(req)
		kind = 1
	} else {
		// 修改
		result = sys_serv.SysTagServ.Update(req)
		kind = 3
	}
	if result != nil && result.RetCode == 1001 {
		var code string
		switch req.Kind {
		case 1:
			code = "10020102"
		case 2:
			code = "10020202"
		}
		logReq := &sys_mod.SysLogSaveReq{UserId: userInfo.UserId, ModCode: code, Kind: kind, OptOrg: userInfo.DeptId}
		sys_serv.SysLogServ.Add(logReq)
		util.ToJson(w, &model.ResJson{Code: 200, Msg: "ok"})
	} else {
		util.ToJson(w, &model.ResJson{Code: 500, Msg: result.RetMsg})
	}
}

// Deletes 删除标签
func (c *sysTag) Deletes(w http.ResponseWriter, r *http.Request) {
	// 将请求的参数转换为请求结构体
	vo, errStr := parse.Parse(r, &sys_mod.SysTagProReq{})
	if vo == nil {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: errStr})
		return
	}
	req := vo.(*sys_mod.SysTagProReq)
	// 通过头部请求的token获取用户信息
	token := r.Header.Get("token")
	userInfo := util.GetUserInfo(token)
	if userInfo == nil {
		util.ToJson(w, &model.ResJson{Code: 401, Msg: "对不起，您未登录或登录超时，请登录！"})
		return
	}
	// 过滤关键参数值中的非法字符
	req.TagIds = util.InputFilter(req.TagIds, true)
	result := sys_serv.SysTagServ.Deletes(req.TagIds)
	if result != nil && result.RetCode == 1001 {
		var code string
		switch req.Kind {
		case 1:
			code = "10020102"
		case 2:
			code = "10020202"
		}
		logReq := &sys_mod.SysLogSaveReq{UserId: userInfo.UserId, ModCode: code, Kind: 1, OptOrg: userInfo.DeptId}
		sys_serv.SysLogServ.Add(logReq)
		util.ToJson(w, &model.ResJson{Code: 200, Msg: "ok"})
	} else {
		util.ToJson(w, &model.ResJson{Code: 500, Msg: result.RetMsg})
	}
}

// Page 获取标签树状列表
func (c *sysTag) Page(w http.ResponseWriter, r *http.Request) {
	// 通过头部请求的token获取用户信息
	token := r.Header.Get("token")
	userInfo := util.GetUserInfo(token)
	if userInfo == nil {
		util.ToJson(w, &model.ResJson{Code: 401, Msg: "对不起，您未登录或登录超时，请登录！"})
		return
	}
	u := r.URL.Query()
	if u == nil {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: "对不起，请求的参数不能为空，请重试"})
		return
	}
	schName := u.Get("schName")
	kinds := u.Get("kind")
	limits := u.Get("limit")
	offsets := u.Get("offset")
	schName = util.InputFilter(schName, false)
	kinds = util.InputFilter(kinds, true)
	limits = util.InputFilter(limits, true)
	offsets = util.InputFilter(offsets, true)
	kind := convert.Str2u8(kinds)
	limit := convert.Str2u8(limits)
	offset := convert.Str2u32(offsets)
	if kind == 0 {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: "对不起，您请求的参数为空，请重试！"})
		return
	}
	// 先统计信息的数量
	req := &sys_mod.SysTagPageReq{SchName: schName, Kind: kind, OptOrg: userInfo.DeptId}
	if result := sys_serv.SysTagServ.Count(req); result != nil && result.RetCode == 1001 && result.RetData.(int64) > 0 {
		res := &sys_mod.SysTagPageRes{Total: result.RetData.(int64)}
		// 再查询具体的信息列表
		req.Limit = limit
		req.Offset = offset
		if result = sys_serv.SysTagServ.Page(req); result != nil && result.RetCode == 1001 {
			res.List = result.RetData
			util.ToJson(w, &model.ResJson{Code: 200, Msg: "ok", Data: res})
			return
		}
	}
	util.ToJson(w, &model.ResJson{Code: 404, Msg: "暂无内容"})
}

// Add 新增标签
func (c *sysTag) Add(w http.ResponseWriter, r *http.Request) {
	// 将请求的参数转换为请求结构体
	vo, errStr := parse.Parse(r, &sys_mod.SysTagSaveReq{})
	if vo == nil {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: errStr})
		return
	}
	req := vo.(*sys_mod.SysTagSaveReq)
	// 通过头部请求的token获取用户信息
	token := r.Header.Get("token")
	userInfo := util.GetUserInfo(token)
	if userInfo == nil {
		util.ToJson(w, &model.ResJson{Code: 401, Msg: "对不起，您未登录或登录超时，请登录！"})
		return
	}
	// 过滤关键参数值中的非法字符
	req.TagName = util.InputFilter(req.TagName, true)
	// 判断标签是否存在
	extVo := &sys_mod.SysTagSelReq{TagName: req.TagName, Kind: req.Kind, OptOrg: userInfo.DeptId}
	if extTag := sys_serv.SysTagServ.Select(extVo); extTag != nil {
		util.ToJson(w, &model.ResJson{Code: 200, Msg: "ok", Data: extTag})
	} else {
		req.TagId = util.NewNanoid()
		req.OptOrg = userInfo.DeptId
		result := sys_serv.SysTagServ.Add(req)
		if result != nil && result.RetCode == 1001 {
			var code string
			switch req.Kind {
			case 1:
				code = "10020102"
			case 2:
				code = "10020202"
			}
			logReq := &sys_mod.SysLogSaveReq{UserId: userInfo.UserId, ModCode: code, Kind: 1, OptOrg: userInfo.DeptId}
			sys_serv.SysLogServ.Add(logReq)
			retTag := &sys_mod.SysTagSelRes{TagId: req.TagId, TagName: req.TagName}
			util.ToJson(w, &model.ResJson{Code: 200, Msg: "ok", Data: retTag})
		} else {
			util.ToJson(w, &model.ResJson{Code: 500, Msg: result.RetMsg})
		}
	}
}

// Delete 删除标签
func (c *sysTag) Delete(w http.ResponseWriter, r *http.Request) {
	// 将请求的参数转换为请求结构体
	vo, errStr := parse.Parse(r, &sys_mod.SysTagDelReq{})
	if vo == nil {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: errStr})
		return
	}
	req := vo.(*sys_mod.SysTagDelReq)
	// 通过头部请求的token获取用户信息
	token := r.Header.Get("token")
	userInfo := util.GetUserInfo(token)
	if userInfo == nil {
		util.ToJson(w, &model.ResJson{Code: 401, Msg: "对不起，您未登录或登录超时，请登录！"})
		return
	}
	// 过滤关键参数值中的非法字符
	req.TagId = util.InputFilter(req.TagId, true)
	result := sys_serv.SysTagServ.Delete(req.TagId)
	if result != nil && result.RetCode == 1001 {
		var code string
		switch req.Kind {
		case 1:
			code = "10020102"
		case 2:
			code = "10020202"
		}
		logReq := &sys_mod.SysLogSaveReq{UserId: userInfo.UserId, ModCode: code, Kind: 2, OptOrg: userInfo.DeptId}
		sys_serv.SysLogServ.Add(logReq)
		util.ToJson(w, &model.ResJson{Code: 200, Msg: "ok"})
	} else {
		util.ToJson(w, &model.ResJson{Code: 500, Msg: result.RetMsg})
	}
}
